package com.example.sxt_practise.IO;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author gaofeng
 * @date 2022年05月08日 17:39
 */
public class ByteArrayTest {
    public static void main(String[] args) {
        // 1.创建源
        byte[] src = new byte[1024] ;
        src = "show me the code".getBytes(StandardCharsets.UTF_8) ;
        // 2.选择流
        InputStream inputStream = new ByteArrayInputStream(src) ;
        try {
            byte[] flush = new byte[1024] ;
            int len = -1 ;
            while ((len = inputStream.read(flush)) != -1) {
                System.out.println(new String(flush, 0 , len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("***********************************");
        // 1.源头内部维护
        byte[] src1 = "oh you are 爆爆".getBytes(StandardCharsets.UTF_8) ;
        // 选择流 不关联源
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ;
        byteArrayOutputStream.write(src1, 0, src1.length);
        try {
            byteArrayOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(new String(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size()));
    }
}
